package com.example.picvideodemo;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * date:2021/7/14
 * author:wsm(admin)
 * funcation:
 */
public class PublicDialog {
    private static Activity mActivity;
    private static PopupWindow mPopupWindow;

    /**
     * 选择相册还是视频
     *
     * @param v
     * @param activity
     */
    @SuppressLint("WrongConstant")
    public static void setphonepop(View v, final Activity activity, final OnButtonClick onButtonClick) {
        mActivity = activity;
        //防止重复按按钮
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            return;
        }
        //设置PopupWindow的View
        View view = LayoutInflater.from(activity).inflate(R.layout.moment_image, null);
        mPopupWindow = new PopupWindow(view, RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
        mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        TextView txtpic = view.findViewById(R.id.click_tp);
        TextView txtvid = view.findViewById(R.id.click_video);
        TextView txtcancel = view.findViewById(R.id.click_cancel);
        //图片
        txtpic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPopupWindow.dismiss();
                if (onButtonClick != null) {
                    onButtonClick.onName("图片");
                }
                setBackgroundAlpha(activity, 1f);
            }
        });
        //视频
        txtvid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPopupWindow.dismiss();
                if (onButtonClick != null) {
                    onButtonClick.onName("视频");
                }
                setBackgroundAlpha(activity, 1f);
            }
        });
        //取消
        txtcancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPopupWindow.dismiss();
                setBackgroundAlpha(activity, 1f);
            }
        });
        mPopupWindow.setFocusable(true);
        mPopupWindow.setAnimationStyle(R.style.PopupWindow);
        mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                mPopupWindow.dismiss();
                setBackgroundAlpha(activity, 1f);
            }
        });
        setBackgroundAlpha(activity, 0.7f);
    }

    //设置屏幕背景透明效果
    public static void setBackgroundAlpha(Activity activity, float alpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = alpha;
        activity.getWindow().setAttributes(lp);
    }
}
